package demo.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import demo.admin.common.BaseResult;
import demo.admin.common.ListResult;
import demo.admin.mapper.ExamplePOMapper;
import demo.admin.model.po.ExamplePO;
import demo.admin.model.vo.ExampleDeleteRequestVO;
import demo.admin.model.vo.ExampleSaveRequestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


@Api(tags = "example")
@Slf4j
@RestController
@RequestMapping("example")
public class ApiExampleController {

    @Resource
    ExamplePOMapper examplePOMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "列表接口")
    @GetMapping("list")
    public BaseResult list(@RequestParam(value = "pageIndex", defaultValue = "0")Integer pageIndex,
                           @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                           @RequestParam(value = "title", required = false)String title) {
        QueryWrapper<ExamplePO> queryWrapper = new QueryWrapper<>();
        if(!StrUtil.isBlank(title)){
            //检索条件
            queryWrapper.like("title", title);
        }
        IPage<ExamplePO> page = examplePOMapper.selectPage(new Page<>(pageIndex, pageSize), queryWrapper);
        return BaseResult.success(new ListResult<>(page.getTotal(), page.getRecords()));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "保存接口")
    @PostMapping("save")
    public BaseResult save(@RequestBody ExampleSaveRequestVO exampleSaveRequestVO)  {
        ExamplePO examplePO = new ExamplePO();
        examplePO.setId(exampleSaveRequestVO.getId());
        examplePO.setTitle(exampleSaveRequestVO.getTitle());
        examplePO.setAuthor(exampleSaveRequestVO.getAuthor());
        examplePO.setCreateTime(exampleSaveRequestVO.getCreateTime());
        examplePO.setImportance(exampleSaveRequestVO.getImportance());
        examplePO.setPageviews(exampleSaveRequestVO.getPageviews());
        examplePO.setContent(exampleSaveRequestVO.getContent());
        examplePOMapper.insertOrUpdate(examplePO);
        return BaseResult.success(null);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除接口")
    @PostMapping("delete")
    public BaseResult delete(@RequestBody ExampleDeleteRequestVO exampleDeleteRequestVO)  {
        if(!exampleDeleteRequestVO.getIds().isEmpty()){
            examplePOMapper.deleteByIds(exampleDeleteRequestVO.getIds());
        }
        return BaseResult.success(null);
    }

}
